<?php

namespace app\mini\controller;

use app\admin\controller\Admin;
use app\admin\model\City;
use app\common\builder\ZBuilder;
use app\mini\model\Lawyer as LawyerModel;
use think\Db;
use think\facade\Cache;

class Lawyer extends Admin
{
    public $model;
    public $cityModel;

    protected function initialize()
    {
        parent::initialize();
        $this->model = new LawyerModel();
        $this->cityModel = new City();
    }

    public function index()
    {
        $map = $this->getMap();
        $order = $this->getOrder();
        $data_list = $this->model->where($map)->order($order)->paginate();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['area'] = $this->getCityName($v['area']);
        }
        $page = $data_list->render();

        $fields = [
            ['hidden', 'id'],
            ['text:6', 'name', '律师姓名'],
            ['text:6', 'number', '执业证号'],
            ['text', 'avatar', '头像地址', '请输入头像局域网地址'],
            ['linkages', 'area', '所在地区', '', 'admin_city', 2],
            ['number:4', 'zx_num', '咨询人数', '', 100],
            ['number:4', 'rate', '用户评分', '', 5.0],
            ['radio:4', 'status', '状态', '', [0 => '禁用', 1 => '启用'], 1],
            ['textarea', 'description', '律师简介'],
        ];

        return ZBuilder::make('table')
            ->addColumns([
                ['name', '姓名', 'text.edit'],
                ['avatar', '头像', 'img_url'],
                ['status', '状态', 'switch'],
                ['number', '执业证号', 'text.edit'],
                ['area', '所在地区'],
                ['mobile', '手机号'],
                ['email', '邮箱'],
                ['zx_num', '咨询人数', 'text.edit'],
                ['rate', '用户评分', 'text.edit'],
                ['right_button', '操作', 'btn'],
            ])
            ->autoAdd($fields)
            ->autoEdit($fields)
            ->addRightButtons(['delete', 'enable', 'disable'])
            ->setRowList($data_list)
            ->setColumnWidth([
                'avatar' => 50,
                'name' => 80,
                'status' => 50,
                'zx_num' => 50,
                'rate' => 50
            ])
            ->setSearch(['name', 'number', 'mobile'])
            ->addOrder('zx_num,rate')
            ->addFilter(['name', 'number'])
            ->addTopSelect('status', '律师状态', [0 => '不显示', 1 => '显示'])
            ->setPages($page)
            ->fetch();
    }

    public function getCityName($code = '', $array = [])
    {
        if (!$code) return '';
        $data = $this->cityModel->where('id', $code)->find();
        if (!$data) return '';
        $array[] = $data->ext_name;
        if ($data->pid !== 0) {
            return $this->getCityName($data->pid, $array);
        }
        return implode('/', array_reverse($array));
    }

    public function edit($id = '')
    {
        if ($id === '') $this->error('参数错误');

        // 获取表单项
        $cache_name = $this->request->module().'/'.parse_name($this->request->controller()).'/edit';
        $cache_name = strtolower($cache_name);
        $form       = Cache::get($cache_name, []);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            print_r($data);die;
        }


        // 获取数据
        $info = Db::name($form['table'])->find($id);

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑')
            ->addFormItems($form['items'])
            ->setFormData($info)
            ->fetch();
    }
}